<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{height: mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <el-button @click="create" type="primary" icon="fa fa-plus"
                                   size="small"> 新增
                        </el-button>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              :data="tableData"
                              :border="true" style="width: 100%">
                        <el-table-column prop="id" label="ID" width="50"></el-table-column>
                        <el-table-column prop="name" label="标示" width="150"></el-table-column>
                        <el-table-column prop="title" label="备注" width="200"></el-table-column>
                        <el-table-column prop="value" label="链接"></el-table-column>
                        <el-table-column label="操作" width="100px">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑</el-button>
                                <!--<el-button icon="fa fa-trash"-->
                                           <!--@click="destory(scope.row)" type="danger" size="mini" plain> 删除-->
                                <!--</el-button>-->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <!-- 新增用户 -->
        <el-dialog
                :title="updateDialogTile"
                custom-class="bh-dialog"
                top="1vh"
                @open="resetForm('dialogForm')"
                :visible.sync="updateDialog"
                width="500px">
            <el-form :model="formData" size="medium" :status-icon="true" :rules="rules" ref="dialogForm" label-width="80px" class="demo-ruleForm">
                <el-form-item></el-form-item>
                <el-form-item required label="标示：" prop="title">
                    <el-input v-model="formData.name"  placeholder="请填写标示"></el-input>
                </el-form-item>
                <el-form-item required label="备注：" prop="title">
                    <el-input v-model="formData.title"  placeholder="请填写备注"></el-input>
                </el-form-item>
                <el-form-item required label="链接：" prop="title">
                    <el-input v-model="formData.value"  placeholder="请填写链接"></el-input>
                </el-form-item>
                <el-button @click="updateDialog = false" style="margin-left:100px;">取 消</el-button>
                <el-button type="primary" @click="update('dialogForm')">确 定</el-button>
            </el-form>
        </el-dialog>
    </el-container>
</template>

<script>
    import { Config, ConfigUpdate } from '../../utils/axios';
    export default {
        name: "index",
        data(){
            return {
                tableData:[],
                updateDialogTile:'',
                updateDialog:false,
                //表单
                formData:{id:'',name: '', title: '', value: '',type:'msg'},
                rules: {
                    name: [{ required: true, message: '请填写标示', trigger: 'blur' },],
                    title: [{ required: true, message: '请填写备注', trigger: 'blur' },],
                    value: [{ required: true, message: '请填写链接地址', trigger: 'blur' },],
                },
            }
        },
        activated(){
            this.list();
        },
        methods:{
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                Config().then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data;
                    }
                })
            },
            // 新增
            create(){
                this.formData = {id:'',name: '', title: '', value: '',type:'msg'};
                this.updateDialog = true;
                this.updateDialogTile = '新增';
            },
            //编辑
            edit(info){
                this.formData = {
                    id: info.id,
                    name: info.name,
                    title: info.title,
                    value: info.value
                };
                this.updateDialogTile = '编辑更新';
                this.updateDialog = true;
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        ConfigUpdate(this.formData).then((response)=>{
                            if(response.code){
                                this.$message.error(response.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose:() =>{
                                        this.updateDialog = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            // destory(info){
            //     this.$confirm('确定要删除该视频信息吗？', '提示', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         VideoDestory({params:{id: info.id}}).then((response) => {
            //             if (response.code) {
            //                 this.$message.error(response.msg);
            //             } else {
            //                 this.list();
            //             }
            //         });
            //     }).catch(() => {});
            // },
            //重置表单
            resetForm(formName) {
                if (this.$refs[formName]!==undefined) {
                    this.$refs[formName].resetFields();
                }
            },
        },
    }
</script>

<style scoped>

</style>
